﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicAdd : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DpStart.Value = "";
                this.DpDeadline.Value = "";
                this.DpReview.Value = "";

                BindHelper.BindTopicType(this.DdlType, null, null, null, false);

                this.UacReviewers.Value = topicBl.GetDefaultReviewer();
                ScriptManager.RegisterStartupScript(this, this.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);

                this.RepDcAndIp.DataSource = topicBl.GetDefaultDCOwner();
                this.RepDcAndIp.DataBind();
                this.OuacIpOwner.Text = topicBl.GetDefaultIPOwner();
            }
        }


        protected void RepDcAndIp_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IdeaType ideaType = (IdeaType)e.Item.DataItem;
                ((Label)e.Item.FindControl("LblDcOwner")).Text = ideaType.Title;
                ((HtmlInputHidden)e.Item.FindControl("HiDcType")).Value = ideaType.Code;
                ((OneUserAutoComplete)e.Item.FindControl("OuacDcOwner")).Text = ideaType.DC;
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            Topic topic = new Topic();
            topic.Title = this.TxtTitle.Text.Trim();
            topic.No = this.TxtNo.Text.Trim();
            topic.Tdc = this.OuacTdc.Text;
            topic.Type = this.DdlType.SelectedValue;
            topic.StartTime = this.DpStart.Date;
            topic.DeadlineTime = this.DpDeadline.Date;
            topic.ReviewTime = this.DpReview.Date;


            Dictionary<string, string> dcUsers = new Dictionary<string,string>();
            foreach(RepeaterItem row in RepDcAndIp.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    string code = (((HtmlInputHidden)row.FindControl("HiDcType"))).Value;
                    dcUsers.Add(code, ((OneUserAutoComplete)row.FindControl("OuacDcOwner")).Text);
                }
            }
            string ipUser = this.OuacIpOwner.Text;

            List<string> reviewers = new List<string>(CodeList.SplitStringToList(this.UacReviewers.Value));
            List<string> mailto = new List<string>(CodeList.SplitStringToList(this.UacMailTo.Value));


            try
            {
                this.topicBl.CreateTopic(topic, dcUsers, ipUser, reviewers, mailto);

                MessageHelper.Show("Success!", this.Page);

                System.Web.UI.ScriptManager.RegisterStartupScript(this.BtnSave, this.Page.GetType(), "Close",
                    String.Format(@"setTimeout(""window.location='{0}';"", 1000);", UrlHelper.GenerateUrl("topiclist")), true);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.Show(aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.Handle(ex);
            }
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            System.Web.UI.ScriptManager.RegisterStartupScript(this.BtnCancel, this.Page.GetType(), "Close",
                @"setTimeout(""window.close();", true);
        }
    }
}